package com.example.famhealing.util;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.example.db.user.user;
import com.example.famhealing.gcm.AdapterAlarm;
import com.example.famhealing.gcm.AlarmData;

public final class Configuration {
	public final static String FamhealingServerURL = "http://192.168.0.26:8080/famhealing/";
	private static Configuration uniqueConfigure = null;
	private static user user = null;
	private static List<FamhealingContacts> contactsList = null;
	private static int exist = 0;
	public static String regId;
	private static SharedPreferences sharedPref;
	public static ArrayList<AlarmData> arrayListAlarmDatas = new ArrayList<AlarmData>();;
	private static AdapterAlarm arrayAdapterAlarmList; 
	
	private Configuration(Context context) {

	}
	
	
	
	public static Configuration getInstance(Context context, user user) {
		if (uniqueConfigure == null) {
			uniqueConfigure = new Configuration(context);

		}
		return uniqueConfigure;
	}	
	public static int getExist() {
		return exist;
	}

	public void setUser(user user) {
		this.user = user;
	}

	public user getUser() {
		return user;
	}

	public static List<FamhealingContacts> getContactsList() {
		return contactsList;
	}

	public static void setContactsList(List<FamhealingContacts> contactsList) {
		Configuration.contactsList = contactsList;
	}

	/*
	 * public void updateExp(){ int currentExp = user.getExp()+1; if(currentExp
	 * == 100){ currentExp = 0; int currentLevel = user.getLevel() + 1;
	 * user.setLevel(currentLevel); } user.setExp(currentExp); }
	 */

	public void updateToServer() {

	}

	public int checkExistServer(String phoneNum, String deviceId) {
		// server로 url 요청
		String url = FamhealingServerURL + "checkUserExist.get?phone_num="
				+ phoneNum +"&device_id="+deviceId;
		Integer exist = 0;
		
		try {
			URL urladdr = new URL(url);

			// Parsing
			boolean inTitle1 = false;
			

			XmlPullParserFactory parserCreator = XmlPullParserFactory
					.newInstance();

			XmlPullParser parser = parserCreator.newPullParser();
			parser.setInput(urladdr.openStream(), "UTF-8");

			int parserEvent = parser.getEventType();

			String tag;

			while (parserEvent != XmlPullParser.END_DOCUMENT) {
				switch (parserEvent) {
				case XmlPullParser.START_DOCUMENT:
					break;
				case XmlPullParser.END_DOCUMENT:
					break;
				case XmlPullParser.START_TAG:
					tag = parser.getName();
					if (tag.equals("exist")) {
						inTitle1 = true;
					}
					break;
				case XmlPullParser.TEXT:
					// tag = parser.getName();
					if (inTitle1) {
						exist = Integer.parseInt(parser.getText());
						inTitle1 = false;
					}
					break;
				}
				parserEvent = parser.next();
			}

		} catch (Exception e) {
			Log.e("dd", "Error in network call", e);
		}
		if(exist == null){
			throw new NullPointerException("Internet access not good");
		}
		
		return exist;
	}

	public static void setPref(SharedPreferences sp){
		sharedPref = sp;
	}

	public static SharedPreferences getPref() {
		// TODO Auto-generated method stub
		return sharedPref;
	}



	public static ArrayList<AlarmData> getArrayListAlarmDatas() {
		return arrayListAlarmDatas;
	}



	public static void addArrayListAlarmDatas(com.example.famhealing.gcm.AlarmData alarmData) {
		Configuration.arrayListAlarmDatas.add(alarmData);
	}



	public static AdapterAlarm getArrayAdapterAlarmList() {
		return arrayAdapterAlarmList;
	}



	public static void setArrayAdapterAlarmList(AdapterAlarm arrayAdapterAlarmList) {
		Configuration.arrayAdapterAlarmList = arrayAdapterAlarmList;
	}
	
}
